<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class OrderRadarXinyan extends Model
{
    protected $table = "sp_order_radar_xinyan";
    protected $primaryKey = 'id';
    public $timestamps = false;

    //查询风控信息
    protected function getRiskData($order_id)
    {
        $data = $this
            ->where('order_id',$order_id)
            ->where("end_at",">",date('Y-m-d H:i:s',time()))
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }
    //商户查询风控信息
    protected function getMemberRiskData($order_id,$member)
    {
        $data = $this
            ->where('order_id',$order_id)
            ->where('member_id',$member)
            ->where("end_at",">",date('Y-m-d H:i:s',time()))
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //添加风控信息
    protected function setRiskData($order_id,$result,$cert_name,$mobile,$cert_no)
    {
        $risk_data['order_id'] = $order_id;
        $risk_data['cert_name'] = $cert_name;
        $risk_data['mobile'] = $mobile;
        $risk_data['cert_no'] = $cert_no;
        $risk_data['result'] = json_encode($result);

        $risk_data['start_at'] = date('Y-m-d H:i:s',time());
        $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);

        //判断数据库没有才添加
        $radar = $this->where('order_id',$order_id)->first();

        $radar = $radar ? $radar->toArray() : array();

        if(empty($radar)){
            $this->insert($risk_data);
        }

        return true;
    }

    //编辑风控信息
    protected function editRiskData($order_id,$risk_data)
    {
        $this->where('order_id',$order_id)->update($risk_data);

        return true;
    }

    protected function editRiskProbeData($order_id,$risk_data)
    {
        $this->where('order_id',$order_id)->update(['risk_probe_result'=>$risk_data]);
        return true;
    }

}
